var textUtils = require("../../utils/textUtils");
var ocrUtils = require("../../utils/ocrUtils");
var clickUtils = require("../../utils/clickUtils");
var randomUtils = require("../../utils/randomUtils");
var adXiGua = require("../../ads/xiGua/index");
var TypeEnum = {
  可领取: "kelingqu",
  立即签到: "立即签到",
};
module.exports = {
  init(type) {
    if (this.overCheck()) {
      return;
    }
    if (this.adCheck()) {
      return;
    }
    if (type != TypeEnum.可领取) {
      if (this.keLingCheck()) {
        return;
      }
    }
    if (type != TypeEnum.立即签到) {
      if (this.signCheck()) {
        return;
      }
    }
  },
  signCheck() {
    var content = "立即签到";
    var TextUI = textUtils.existsTextStartsWith(content);
    if (TextUI) {
      clickUtils.clickTextUI(TextUI);
      sleep(randomUtils.getRandom(2.5, 5, 1000));
      this.init(TypeEnum.立即签到);
      return true;
    }
    var TextOCR = ocrUtils.captureExistsText(content);
    if (TextOCR) {
      clickUtils.clickTextOCR(TextOCR);
      sleep(randomUtils.getRandom(2.5, 5, 1000));
      this.init(TypeEnum.立即签到);
      return true;
    }
    return false;
  },
  keLingCheck() {
    var content = "可领取";
    var TextUI = textUtils.existsTextStartsWith(content);
    if (TextUI) {
      clickUtils.clickTextUI(TextUI);
      sleep(randomUtils.getRandom(2.5, 5, 1000));
      this.init(TypeEnum.可领取);
      return true;
    }
    var TextOCR = ocrUtils.captureExistsText(content);
    if (TextOCR) {
      clickUtils.clickTextOCR(TextOCR);
      sleep(randomUtils.getRandom(2.5, 5, 1000));
      this.init(TypeEnum.可领取);
      return true;
    }
    return false;
  },
  overCheck() {
    var contents = [
      //prettier-ignore
      "开心收下",
      "立即收下",
    ];
    var TextUI = textUtils.existsOneTextStartsWith(contents);
    if (TextUI) {
      clickUtils.clickTextUI(TextUI);
      sleep(randomUtils.getRandom(1.5, 2, 1000));
      return true;
    }
    var TextOCR = ocrUtils.captureExistsOneText(contents);
    if (TextOCR) {
      clickUtils.clickTextOCR(TextOCR);
      sleep(randomUtils.getRandom(1.5, 2, 1000));
      return true;
    }
    return false;
  },

  adCheck() {
    var contents = [
      //prettier-ignore
      "看广告视频得",
      "看广告视频领",
      "看广告视频再得",
      "看广告视频再领",
    ];
    var TextUI = textUtils.existsOneTextStartsWith(contents);
    if (TextUI) {
      clickUtils.clickTextUI(TextUI);
      sleep(randomUtils.getRandom(3.5, 5, 1000));
      var maxAdCount = randomUtils.getRandom(2, 5);
      adXiGua.startWatchAd(maxAdCount);
      this.init();
      return true;
    }
    var TextOCR = ocrUtils.captureExistsOneText(contents);
    if (TextOCR) {
      clickUtils.clickTextOCR(TextOCR);
      sleep(randomUtils.getRandom(3.5, 5, 1000));
      var maxAdCount = randomUtils.getRandom(2, 5);
      adXiGua.startWatchAd(maxAdCount);
      this.init();
      return true;
    }
    return false;
  },
};
